const dao = require('./base');

module.exports = {

    chaincodeList: async function (condition, fields = "*", by = []) {
        let sql = `SELECT ${fields} from afc_chaincode_view where del_flag=0`;
        let pre = [];

        if (condition.cid !== undefined && condition.cid !== '') {
            sql += ` and cid = ?`;
            pre.push(condition.cid);
        }
        if (condition.cc_type !== undefined && condition.cc_type !== '') {
            sql += ` and cc_type = ?`;
            pre.push(condition.cc_type);
        }
        if (condition.cc_status !== undefined && condition.cc_status !== '') {
            sql += ` and cc_status = ?`;
            pre.push(condition.cc_status);
        }
        sql = dao.by(sql, by)

        let limit = dao.limit(sql, condition, pre);
        sql = limit.sql;
        pre = limit.pre;

        let data;
        await dao.query(sql, pre).then((results, fields) => {
            data = results;
        }).catch((err) => {
            data = {err: err}
        });
        return data;
    },

    chaincodeAdd: async function (fields = []) {
        let sql = "insert into afc_chaincode (cid,chaincode,mid,cc_type,ca_id_search,ca_id_decode,cc_status) values(?,?,?,?,?,?,?)";
        let pre = [];
        let data;
        pre.push(fields.cid);
        pre.push(fields.chaincode);
        pre.push(fields.mid);
        pre.push(fields.cc_type);
        pre.push(fields.ca_id_search);
        pre.push(fields.ca_id_decode);
        pre.push(fields.cc_status);

        await dao.query(sql, pre).then((results, fields) => {
            data = results;
        }).catch((err) => {
            data = {err: err}
        });
        return data;
    },

    chaincodeUpdate: async function (condition = {}, fields = {}) {
        let sql = "update afc_chaincode set ";
        let pre = [];
        let data;
        if (fields.cid !== undefined && fields.cid !== '') {
            sql = dao.update_sql_splice(sql, "cid");
            pre.push(fields.cid);
        }
        if (fields.chaincode !== undefined && fields.chaincode !== '') {
            sql = dao.update_sql_splice(sql, "chaincode");
            pre.push(fields.chaincode);
        }
        if (fields.mid !== undefined && fields.mid !== '') {
            sql = dao.update_sql_splice(sql, "mid");
            pre.push(fields.mid);
        }
        if (fields.cc_type !== undefined && fields.cc_type !== '') {
            sql = dao.update_sql_splice(sql, "cc_type");
            pre.push(fields.cc_type);
        }
        if (fields.cc_status !== undefined && fields.cc_status !== '') {
            sql = dao.update_sql_splice(sql, "cc_status");
            pre.push(fields.cc_status);
        }
        if (fields.ca_id_search !== undefined && fields.ca_id_search !== '') {
            sql = dao.update_sql_splice(sql, "ca_id_search");
            pre.push(fields.ca_id_search);
        }
        if (fields.ca_id_decode !== undefined && fields.ca_id_decode !== '') {
            sql = dao.update_sql_splice(sql, "ca_id_decode");
            pre.push(fields.ca_id_decode);
        }
        if (fields.del_flag !== undefined && fields.del_flag !== '') {
            sql = dao.update_sql_splice(sql, "del_flag");
            pre.push(fields.del_flag);
        }
        if (condition.cc_id !== undefined && condition.cc_id !== '') {
            sql += ` where cc_id = ?`;
            pre.push(condition.cc_id);
        } else {
            return;
        }

        await dao.query(sql, pre).then((results, fields) => {
            data = results;
        }).catch((err) => {
            data = {err: err}
        });
        return data;
    },


};
